<?php
namespace app\common\model;
use think\Model;
use think\Validate;
use app\common\model\PageCategory;

class Page extends Model
{
	protected $table = 'page';
	protected $pk = 'page_id';
	protected $autoWriteTimestamp = true;
	protected $createTime = 'page_create_time';
	protected $updateTime = false;

	// 验证规则
	protected $rule = [
		['page_title|文章标题', ['require']],
		['page_content|文章内容', ['require']],
		['page_cate_id|文章所属分类', ['require']],
	]; 
	
	// 验证场景
	protected $scene = [
        'insert' => ['page_title', 'page_content', 'page_cate_id'],
        'update' => ['page_title', 'page_content', 'page_cate_id'],
    ];

    // 验证器
	public function intValidate(){
        $validate = new \think\Validate($this->rule);
        foreach ($this->scene as $k1=>$v1)
            $validate->scene($k1, $v1);
        return $validate;
    }

	public static function showlist()
    {
        
        $data = Page::alias('a')
                ->field('a.page_title_english,a.page_id,a.page_title,a.page_content,a.page_cate_id,b.page_category_id,b.page_category_name')
                ->join('page_category b', 'a.page_cate_id=b.page_category_id', 'left')
                ->order('page_id', 'desc')
                ->where('page_is_delete', 0)
                ->select();

        return $data;
    }

    public static function getPageById($id)
    {
        $where['page_is_delete'] = ['eq', 0];
        $where['page_id'] = ['eq', $id];
    	$data = Page::field('page_title_english,page_title,page_content,page_cate_id')->where($where)->find();
    	return $data;
    } 


    public static function changeDel($pageId) 
    {
    	$result = Page::where('page_id', $pageId)->setField('page_is_delete', 1);
    	if(!$result)
            return false;

        return true;
    }


    public static function getPages($pageCatId)
    {
        $where = [
            'page_cate_id' => $pageCatId,
            'page_is_delete' => 0
        ];
        $data = Page::field('page_title_english,page_id,page_title,page_content,page_cate_id')->where($where)->select();

        return $data;
    }


    public static function getPageContent($id)
    {
        $where = [
            'page_id' => $id,
            'page_is_delete' => 0
        ];
        
        $data = Page::field('page_title_english,page_id,page_title,page_content,page_cate_id')->where($where)->find();

        return $data;
    }

}


